Unity 模型尺寸自适应 您所在的位置:网站首页 unity 不跟着变化大小 Unity 模型尺寸自适应

Unity 模型尺寸自适应

2024-06-18 21:37| 来源: 网络整理| 查看: 265

在开发过程中可能会因为美术资源规格不统一或者其他项目上的硬性需求遇到模型在被加载出以后大小不一的情况,基本的解决方案都是修改scale值进行直接的缩放,但是有时量级太多无法判定scale的数值时就需要针对模型的render进行调节,以下为详细的解决方案

一、计算模型的标准尺寸

在适配前,先对标准的尺寸进行计算,即选定想要适配的标准模型,对该模型进行参数的抽取作为标准参数。

/// /// 获取标准模型参数 /// /// 目标游戏物体 /// private Vector3 GetNormalSize(GameObject gameObj) { Vector3 modelSize = Vector3.zero; allChild = gameObj.GetComponentsInChildren(); for (int i = 0; i < allChild.Length; i++) { if (allChild[i].GetComponent()) { //获取模型的规格尺寸 modelSize = allChild[i].GetComponent().bounds.size; } if (allChild[i].GetComponent()) { modelSize = allChild[i].GetComponent().bounds.size; return modelSize; } } return modelSize; } 二、针对render类型对模型的Scale进行缩放

获取到标准模型参数后可以对需要做适配的模型进行数据的适配,具体的参数可以做限制,基本原理就是依据标准的模型参数数据做比值,依据比值进行scale的缩放,两层计算后就可以得到对应的适配效果。

/// /// 设置模型尺寸 /// /// 模型游戏物体 private void SetModelSize(GameObject gameObj) { allChild = gameObj.GetComponentsInChildren(); for (int i = 0; i < allChild.Length; i++) { if (allChild[i].GetComponent()) { Vector3 modelSize; //获取模型的规格尺寸 modelSize = allChild[i].GetComponent().bounds.size; //限定模型尺寸的规格 if (modelSize.y < 2) { gameObj.transform.localScale = Vector3.one * 10; } else { gameObj.transform.localScale = new Vector3( gameObj.transform.localScale.x / (modelSize.x / 1f), gameObj.transform.localScale.y / (modelSize.x / 1f), gameObj.transform.localScale.z / (modelSize.x / 1f) ); } } if (allChild[i].GetComponent()) { Vector3 modelSize; modelSize = allChild[i].GetComponent().bounds.size; if (modelSize.y < 2) { gameObj.transform.localScale = Vector3.one * 10; } else { gameObj.transform.localScale = new Vector3( gameObj.transform.localScale.x / (modelSize.x / 1f), gameObj.transform.localScale.y / (modelSize.x / 1f), gameObj.transform.localScale.z / (modelSize.x / 1f) ); } Debug.Log(modelSize); Debug.Log(gameObj.transform.localScale); } } }

【这种粗糙的计算方式会导致部分模型的缩放出现拉伸变形的效果,需要对模型进行细节上的计算来最大化保证模型的表现效果】



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有